package user

import (
	"context"
	"errors"
	"ns/app/applet/api/internal/code"
	"ns/app/applet/api/internal/model"

	"ns/app/applet/api/internal/svc"
	"ns/app/applet/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryUserDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewQueryUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUserDetailLogic {
	return &QueryUserDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *QueryUserDetailLogic) QueryUserDetail(req *types.QueryUserDetailRequest) (resp *types.QueryUserDetailResponse, err error) {
	user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
	if err != nil {
		if errors.Is(err, model.ErrNotFound) {
			return nil, code.ErrUserNotFound
		}
		return nil, err
	}
	return &types.QueryUserDetailResponse{
		Detail: types.UserDetail{
			Id:       user.Id,
			Name:     user.Name,
			Mobile:   user.Mobile,
			Email:    user.Email,
			Level:    int32(user.Level),
			Province: user.Province,
			City:     user.City.String,
			Area:     user.Area.String,
			Type:     user.Type,
		},
	}, nil
}
